local skynet = require "skynet"
local sharedata = require "skynet.sharedata"
local zjh_rule = require "zhajinhua.zjh_rule"
local hhdz = require "zhajinhua.hhdz"

local table_insert = table.insert
local math_random = math.random
local table_remove = table.remove
local hhdz_cards = {}

local function gen_hhd_cards()
	local cards_list = {}
	local hhdz_cards = {}
    for i = 1,13 do
        for j = 1,4 do
            table_insert(cards_list,zjh_rule.make_card_id(j,i))
        end
	end

	for i = 1,51 do
		for j = i+1,51 do
			for k = j+1,51 do
				local cards = {cards_list[i],cards_list[j],cards_list[k]}
				local card_type = hhdz.get_card_type(cards)
				if not hhdz_cards[card_type] then
					hhdz_cards[card_type] = {}
					table_insert(hhdz_cards[card_type], cards)
				else
					table_insert(hhdz_cards[card_type], cards)
				end
			end
		end
	end
	return hhdz_cards
end


local function is_crads_equal(cards, other_cards)
	for _, card in pairs(cards) do
    	for _, other_card in pairs(other_cards) do
    		if card == other_card then
    			return true
    		end
    	end
    end
    return false
end




















local internal = {}

function internal.get_cards(_,_, card_type, random)
	dbglog('cards_library___get_cards', card_type)
	local card_list = hhdz_cards[card_type]
	if random then 
		local card_index = math_random(1,#card_list)
		local cards = card_list[card_index]		
		return skynet.retpack(cards)
	end
	return skynet.retpack(card_list)
end


function internal.get_side_no_normal_card(_, _, card_type, other_card_type, side)

	local card_list = hhdz_cards[card_type]
	local card_index = math_random(1,#card_list)
	local cards = card_list[card_index]

	local other_card_list = hhdz_cards[other_card_type]
	print("___internel_hhdz_cards____33333__", card_type, other_card_type)

	local find = false
	local other_index
	local i = 1
	repeat
		other_index = math_random(1,#other_card_list)
        -- other_index = index_list[index]
        local other_cards = other_card_list[other_index]
        local is_equal = is_crads_equal(cards, other_cards) --判断两幅牌是否存在相同点数的牌
        if not is_equal then
        	if not zjh_rule.is_draw(cards,other_cards) then --不能出现平局
        		find = true
        	end
        end
        if not find then
        	i = i + 1
        end
    until(i >= 50 or find)
    print("forge find get==========",i)
    --50次如果找不到强制搜索
    if i >= 50 and not find then
    	for index, other_cards in pairs(other_card_list) do
    		local is_equal = is_crads_equal(cards, other_cards)
    		if not is_equal then
    			if not zjh_rule.is_draw(cards,other_cards) then
    				other_index = index
    				break
    			end
    		end
    	end
   	end
	local red_card_type = zjh_rule.get_card_type(cards)
	local black_card_type = zjh_rule.get_card_type(other_card_list[other_index])
	local win = zjh_rule.compare_card(red_card_type, cards, black_card_type, other_card_list[other_index])
	if (win and side == 1) or (not win and side == 2) then
		return skynet.retpack(cards, other_card_list[other_index])
	end
	
	if (not win and side == 1 ) or (win and side == 2) then
		return skynet.retpack(other_card_list[other_index], cards)
	end
	
	--再做一次随机
    local side_random = math_random(1,100)
    if side_random >= 50 then
		return skynet.retpack(cards, other_card_list[other_index])
	else
		return skynet.retpack(other_card_list[other_index], cards)
	end
	
end



local function handle_request(session,source,action,...)
    dbglog('cards_library___action', action,...)
    local f = assert(internal[action],'unknown action ' .. action)
    f(session,source,...)
end

skynet.start(function()
    -- local hhdz_cards = gen_hhd_cards() --生成红黑牌库
    -- sharedata.update("internel_hhdz_cards", hhdz_cards)
    -- skynet.exit()
    hhdz_cards = gen_hhd_cards() --生成红黑牌库

	skynet.dispatch("lua",function(session,source,...)
		handle_request(session,source,...)
	end)
end)
